package com.sunni.controller;

import com.sunni.entity.Admin;
import com.sunni.entity.Reader;
import com.sunni.service.LoginService;
import com.sunni.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    static LoginService loginService = new LoginServiceImpl();

//    public static void main(String[] args) {
//        System.out.println("LoginServlet控制层："+loginService.login("wuwukai","123"));
//    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String type = req.getParameter("type");

        Object object = loginService.login(username, password,type);
        if (object!=null){
                HttpSession session = req.getSession();
                switch (type){
                    case "reader":
                        Reader reader = (Reader) object;
                        session.setAttribute("reader",reader);
                        resp.sendRedirect("/bookFindAll");
                        break;
                    case "admin":
                        Admin admin = (Admin) object;
                        session.setAttribute("admin",admin);
                        resp.sendRedirect("/admin");
                        break;
                }
        }else {
                resp.sendRedirect("/login.jsp");
        }
    }

}
